package com.test.atomikost.controller;

import com.alibaba.fastjson.JSONObject;
import com.test.atomikost.model.TAccessPoint;
import com.test.atomikost.service.IAccessPointService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 16-3-29.
 */
@Controller
@RequestMapping(value="/{domain}/accesspiont/")
public class AccessPointController {

    @Resource
    private IAccessPointService accessPointService;

    @RequestMapping(value = "query_access_point_id",method = RequestMethod.POST)
    public void queryAccessPointId(HttpServletRequest request,HttpServletResponse response,@PathVariable String domain,@RequestBody String params){
        System.out.print(domain);
        //String id = request.getParameter("id");
        String accessPointId = "AccessPoint";
        String bmzzId ="23DQE/BMZZ0001";

        List<Map<String,String>> list  = accessPointService.queryAccessPointId(accessPointId,bmzzId);
        printOutSuccess(response, list);
    }
    @RequestMapping(value = "save_data.do",method = RequestMethod.POST)
    public void saveData(HttpServletRequest request,HttpServletResponse response){
       String falg  = accessPointService.saveData();
        printOutSuccess(response, falg);
    }



    /**
     * 正常返回数据
     * @param response
     * @param data 返回的数据
     */
    protected void printOutSuccess(HttpServletResponse response,Object data){

        JSONObject _msg = new JSONObject();
        _msg.put("code",0);
        _msg.put("data", data);
        printOut(response,_msg);
    }

    /**
     * 输出响应流
     * @param response
     * @param content：要输出的内容
     * @return
     */
    public static void printOut(HttpServletResponse response, Object content) {
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            // response.setContentLength(responseContent.length());
            writer.write(content.toString());
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

}
